﻿using System;
using System.Collections.Generic;
using System.Linq;
using Citi.Import.Util;
using Citi.ResourceConsolidation.Domain.Utils;

namespace Citi.ResourceConsolidation.Domain
{
    public partial class Location
    {
        private const string WorkStateColumn = "Work State/Province";
        private const string WorkCityColumn = "Work City"; // Description
        private const string LocationColumn = "Location"; // Code
        private const string PhysicalCountryColumn = "Physical Country";
        private const string PhysicalRegionColumn = "Physical Region";

        public static readonly List<Definition<Location>> Definitions = new List<Definition<Location>>
            {
                new Definition<Location> { ModelProperty = x => x.Code, MaxLength = 50, Required = false, ColumnName = LocationColumn, CustomConversionMethod = ValidateCode},
                new Definition<Location> { ModelProperty = x => x.Description, MaxLength = 300, Required = false, ColumnName = WorkCityColumn},
                new Definition<Location> { ModelProperty = x => x.State, MaxLength = 50, Required = false, ColumnName = WorkStateColumn},
                new Definition<Location> { ModelProperty = x => x.Country, MaxLength = 300, Required = false, ColumnName = PhysicalCountryColumn},
                new Definition<Location> { ModelProperty = x => x.Region, MaxLength = 50, Required = false  , ColumnName = PhysicalRegionColumn},
            };

        private static object ValidateCode(ref Location model, string value, List<string> fields, ref string error, List<string> headers = null)
        {
            if (String.IsNullOrEmpty(value))
            {
                error = String.Format("The field {0} can not be null", LocationColumn);
                return Conversion.Validation.Failed;
            }

            var domain = CacheFacade.Get(new Location().SelectAll<Location>).FirstOrDefault(x => x.Code == value);

            if (domain != null)
            {
                model = domain;
                return Conversion.Validation.Validated;
            }

            return value;
        }

        public static void Save(List<Location> locations)
        {
            locations = locations.Distinct(new LocationComparer()).Where(x => x != null && !String.IsNullOrEmpty(x.Code) && x.Id == 0).ToList();

            if (!locations.Any()) return;
            locations.First().Save(locations, 100);

            CacheFacade.Clear(typeof(Location));
            Context.ClearContext();
        }
    }

    public class LocationComparer : IEqualityComparer<Location>
    {
        public bool Equals(Location x, Location y)
        {
            if (ReferenceEquals(x, y)) return true;
            if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
                return false;

            return x.Description == y.Description && x.Id == y.Id && x.Code == y.Code && x.Country == y.Country;
        }

        public int GetHashCode(Location item)
        {
            if (ReferenceEquals(item, null)) return 0;

            int hashDescription = item.Description == null ? 0 : item.Description.GetHashCode();
            int hashId = item.Description == null ? 0 : item.Id.GetHashCode();
            int hashCode = item.Code == null ? 0 : item.Code.GetHashCode();

            return hashDescription ^ hashId ^ hashCode;
        }
    }
}
